home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / elisp-9 (.txt) < prev    next >
GNU Info File  |  1993-06-01  |  51KB  |  965 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.    This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
  4. Emacs Version 19.
  5.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  6. Cambridge, MA 02139 USA
  7.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.    Permission is granted to copy and distribute modified versions of
  12. this manual under the conditions for verbatim copying, provided that
  13. the entire resulting derived work is distributed under the terms of a
  14. permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions, except that this permission notice may be stated in a
  18. translation approved by the Foundation.
  19. File: elisp,  Node: Calling Functions,  Next: Mapping Functions,  Prev: Defining Functions,  Up: Functions
  20. Calling Functions
  21. =================
  22.    Defining functions is only half the battle.  Functions don't do
  23. anything until you "call" them, i.e., tell them to run.  This process
  24. is also known as "invocation".
  25.    The most common way of invoking a function is by evaluating a list.
  26. For example, evaluating the list `(concat "a" "b")' calls the function
  27. `concat'.  *Note Evaluation::, for a description of evaluation.
  28.    When you write a list as an expression in your program, the function
  29. name is part of the program.  This means that the choice of which
  30. function to call is made when you write the program.  Usually that's
  31. just what you want.  Occasionally you need to decide at run time which
  32. function to call.  Then you can use the functions `funcall' and `apply'.
  33.  - Function: funcall FUNCTION &rest ARGUMENTS
  34.      `funcall' calls FUNCTION with ARGUMENTS, and returns whatever
  35.      FUNCTION returns.
  36.      Since `funcall' is a function, all of its arguments, including
  37.      FUNCTION, are evaluated before `funcall' is called.  This means
  38.      that you can use any expression to obtain the function to be
  39.      called.  It also means that `funcall' does not see the expressions
  40.      you write for the ARGUMENTS, only their values.  These values are
  41.      *not* evaluated a second time in the act of calling FUNCTION;
  42.      `funcall' enters the normal procedure for calling a function at the
  43.      place where the arguments have already been evaluated.
  44.      The argument FUNCTION must be either a Lisp function or a
  45.      primitive function.  Special forms and macros are not allowed,
  46.      because they make sense only when given the "unevaluated" argument
  47.      expressions.  `funcall' cannot provide these because, as we saw
  48.      above, it never knows them in the first place.
  49.           (setq f 'list)
  50.                => list
  51.           (funcall f 'x 'y 'z)
  52.                => (x y z)
  53.           (funcall f 'x 'y '(z))
  54.                => (x y (z))
  55.           (funcall 'and t nil)
  56.           error--> Invalid function: #<subr and>
  57.      Compare this example with that of `apply'.
  58.  - Function: apply FUNCTION &rest ARGUMENTS
  59.      `apply' calls FUNCTION with ARGUMENTS, just like `funcall' but
  60.      with one difference: the last of ARGUMENTS is a list of arguments
  61.      to give to FUNCTION, rather than a single argument.  We also say
  62.      that this list is "appended" to the other arguments.
  63.      `apply' returns the result of calling FUNCTION.  As with
  64.      `funcall', FUNCTION must either be a Lisp function or a primitive
  65.      function; special forms and macros do not make sense in `apply'.
  66.           (setq f 'list)
  67.                => list
  68.           (apply f 'x 'y 'z)
  69.           error--> Wrong type argument: listp, z
  70.           (apply '+ 1 2 '(3 4))
  71.                => 10
  72.           (apply '+ '(1 2 3 4))
  73.                => 10
  74.           
  75.           (apply 'append '((a b c) nil (x y z) nil))
  76.                => (a b c x y z)
  77.      An interesting example of using `apply' is found in the description
  78.      of `mapcar'; see the following section.
  79.    It is common for Lisp functions to accept functions as arguments or
  80. find them in data structures (especially in hook variables and property
  81. lists) and call them using `funcall' or `apply'.  Functions that accept
  82. function arguments are often called "functionals".
  83.    Sometimes, when you call such a function, it is useful to supply a
  84. no-op function as the argument.  Here are two different kinds of no-op
  85. function:
  86.  - Function: identity ARG
  87.      This function returns ARG and has no side effects.
  88.  - Function: ignore &rest ARGS
  89.      This function ignores any arguments and returns `nil'.
  90. File: elisp,  Node: Mapping Functions,  Next: Anonymous Functions,  Prev: Calling Functions,  Up: Functions
  91. Mapping Functions
  92. =================
  93.    A "mapping function" applies a given function to each element of a
  94. list or other collection.  Emacs Lisp has three such functions;
  95. `mapcar' and `mapconcat', which scan a list, are described here.  For
  96. the third mapping function, `mapatoms', see *Note Creating Symbols::.
  97.  - Function: mapcar FUNCTION SEQUENCE
  98.      `mapcar' applies FUNCTION to each element of SEQUENCE in turn.
  99.      The results are made into a `nil'-terminated list.
  100.      The argument SEQUENCE may be a list, a vector or a string.  The
  101.      result is always a list.  The length of the result is the same as
  102.      the length of SEQUENCE.
  103.      For example:
  104.           (mapcar 'car '((a b) (c d) (e f)))
  105.                => (a c e)
  106.           (mapcar '1+ [1 2 3])
  107.                => (2 3 4)
  108.           (mapcar 'char-to-string "abc")
  109.                => ("a" "b" "c")
  110.           ;; Call each function in `my-hooks'.
  111.           (mapcar 'funcall my-hooks)
  112.           (defun mapcar* (f &rest args)
  113.             "Apply FUNCTION to successive cars of all ARGS, until one ends.
  114.           Return the list of results."
  115.             ;; If no list is exhausted,
  116.             (if (not (memq 'nil args))
  117.                 ;; Apply function to CARs.
  118.                 (cons (apply f (mapcar 'car args))
  119.                       (apply 'mapcar* f
  120.                              ;; Recurse for rest of elements.
  121.                              (mapcar 'cdr args)))))
  122.           (mapcar* 'cons '(a b c) '(1 2 3 4))
  123.                => ((a . 1) (b . 2) (c . 3))
  124.  - Function: mapconcat FUNCTION SEQUENCE SEPARATOR
  125.      `mapconcat' applies FUNCTION to each element of SEQUENCE: the
  126.      results, which must be strings, are concatenated.  Between each
  127.      pair of result strings, `mapconcat' inserts the string SEPARATOR.
  128.      Usually SEPARATOR contains a space or comma or other suitable
  129.      punctuation.
  130.      The argument FUNCTION must be a function that can take one
  131.      argument and returns a string.
  132.           (mapconcat 'symbol-name
  133.                      '(The cat in the hat)
  134.                      " ")
  135.                => "The cat in the hat"
  136.           (mapconcat (function (lambda (x) (format "%c" (1+ x))))
  137.                      "HAL-8000"
  138.                      "")
  139.                => "IBM.9111"
  140. File: elisp,  Node: Anonymous Functions,  Next: Function Cells,  Prev: Mapping Functions,  Up: Functions
  141. Anonymous Functions
  142. ===================
  143.    In Lisp, a function is a list that starts with `lambda' (or
  144. alternatively a primitive subr-object); names are "extra".  Although
  145. usually functions are defined with `defun' and given names at the same
  146. time, it is occasionally more concise to use an explicit lambda
  147. expression--an anonymous function.  Such a list is valid wherever a
  148. function name is.
  149.    Any method of creating such a list makes a valid function.  Even
  150. this:
  151.      (setq silly (append '(lambda (x)) (list (list '+ (* 3 4) 'x))))
  152.           => (lambda (x) (+ 12 x))
  153. This computes a list that looks like `(lambda (x) (+ 12 x))' and makes
  154. it the value (*not* the function definition!) of `silly'.
  155.    Here is how we might call this function:
  156.      (funcall silly 1)
  157.           => 13
  158. (It does *not* work to write `(silly 1)', because this function is not
  159. the *function definition* of `silly'.  We have not given `silly' any
  160. function definition, just a value as a variable.)
  161.    Most of the time, anonymous functions are constants that appear in
  162. your program.  For example, you might want to pass one as an argument
  163. to the function `mapcar', which applies any given function to each
  164. element of a list.  Here we pass an anonymous function that multiplies
  165. a number by two:
  166.      (defun double-each (list)
  167.        (mapcar '(lambda (x) (* 2 x)) list))
  168.           => double-each
  169.      (double-each '(2 11))
  170.           => (4 22)
  171. In such cases, we usually use the special form `function' instead of
  172. simple quotation to quote the anonymous function.
  173.  - Special Form: function FUNCTION-OBJECT
  174.      This special form returns FUNCTION-OBJECT without evaluating it.
  175.      In this, it is equivalent to `quote'.  However, it serves as a
  176.      note to the Emacs Lisp compiler that FUNCTION-OBJECT is intended
  177.      to be used only as a function, and therefore can safely be
  178.      compiled.  *Note Quoting::, for comparison.
  179.    Using `function' instead of `quote' makes a difference inside a
  180. function or macro that you are going to compile.  For example:
  181.      (defun double-each (list)
  182.        (mapcar (function (lambda (x) (* 2 x))) list))
  183.           => double-each
  184.      (double-each '(2 11))
  185.           => (4 22)
  186. If this definition of `double-each' is compiled, the anonymous function
  187. is compiled as well.  By contrast, in the previous definition where
  188. ordinary `quote' is used, the argument passed to `mapcar' is the
  189. precise list shown:
  190.      (lambda (arg) (+ arg 5))
  191. The Lisp compiler cannot assume this list is a function, even though it
  192. looks like one, since it does not know what `mapcar' does with the
  193. list.  Perhaps `mapcar' will check that the CAR of the third element is
  194. the symbol `+'!  The advantage of `function' is that it tells the
  195. compiler to go ahead and compile the constant function.
  196.    We sometimes write `function' instead of `quote' when quoting the
  197. name of a function, but this usage is just a sort of comment.
  198.      (function SYMBOL) == (quote SYMBOL) == 'SYMBOL
  199.    See `documentation' in *Note Accessing Documentation::, for a
  200. realistic example using `function' and an anonymous function.
  201. File: elisp,  Node: Function Cells,  Next: Inline Functions,  Prev: Anonymous Functions,  Up: Functions
  202. Accessing Function Cell Contents
  203. ================================
  204.    The "function definition" of a symbol is the object stored in the
  205. function cell of the symbol.  The functions described here access, test,
  206. and set the function cell of symbols.
  207.  - Function: symbol-function SYMBOL
  208.      This returns the object in the function cell of SYMBOL.  If the
  209.      symbol's function cell is void, a `void-function' error is
  210.      signaled.
  211.      This function does not check that the returned object is a
  212.      legitimate function.
  213.           (defun bar (n) (+ n 2))
  214.                => bar
  215.           (symbol-function 'bar)
  216.                => (lambda (n) (+ n 2))
  217.           (fset 'baz 'bar)
  218.                => bar
  219.           (symbol-function 'baz)
  220.                => bar
  221.    If you have never given a symbol any function definition, we say that
  222. that symbol's function cell is "void".  In other words, the function
  223. cell does not have any Lisp object in it.  If you try to call such a
  224. symbol as a function, it signals a `void-function' error.
  225.    Note that void is not the same as `nil' or the symbol `void'.  The
  226. symbols `nil' and `void' are Lisp objects, and can be stored into a
  227. function cell just as any other object can be (and they can be valid
  228. functions if you define them in turn with `defun'); but `nil' or `void'
  229. is *an object*.  A void function cell contains no object whatsoever.
  230.    You can test the voidness of a symbol's function definition with
  231. `fboundp'.  After you have given a symbol a function definition, you
  232. can make it void once more using `fmakunbound'.
  233.  - Function: fboundp SYMBOL
  234.      Returns `t' if the symbol has an object in its function cell,
  235.      `nil' otherwise.  It does not check that the object is a legitimate
  236.      function.
  237.  - Function: fmakunbound SYMBOL
  238.      This function makes SYMBOL's function cell void, so that a
  239.      subsequent attempt to access this cell will cause a `void-function'
  240.      error.  (See also `makunbound', in *Note Local Variables::.)
  241.           (defun foo (x) x)
  242.                => x
  243.           (fmakunbound 'foo)
  244.                => x
  245.           (foo 1)
  246.           error--> Symbol's function definition is void: foo
  247.  - Function: fset SYMBOL OBJECT
  248.      This function stores OBJECT in the function cell of SYMBOL.  The
  249.      result is OBJECT.  Normally OBJECT should be a function or the
  250.      name of a function, but this is not checked.
  251.      There are three normal uses of this function:
  252.         * Copying one symbol's function definition to another.  (In
  253.           other words, making an alternate name for a function.)
  254.         * Giving a symbol a function definition that is not a list and
  255.           therefore cannot be made with `defun'.  *Note Classifying
  256.           Lists::, for an example of this usage.
  257.         * In constructs for defining or altering functions.  If `defun'
  258.           were not a primitive, it could be written in Lisp (as a
  259.           macro) using `fset'.
  260.      Here are examples of the first two uses:
  261.           ;; Give `first' the same definition `car' has.
  262.           (fset 'first (symbol-function 'car))
  263.                => #<subr car>
  264.           (first '(1 2 3))
  265.                => 1
  266.           
  267.           ;; Make the symbol `car' the function definition of `xfirst'.
  268.           (fset 'xfirst 'car)
  269.                => car
  270.           (xfirst '(1 2 3))
  271.                => 1
  272.           (symbol-function 'xfirst)
  273.                => car
  274.           (symbol-function (symbol-function 'xfirst))
  275.                => #<subr car>
  276.           
  277.           ;; Define a named keyboard macro.
  278.           (fset 'kill-two-lines "\^u2\^k")
  279.                => "\^u2\^k"
  280.    When writing a function that extends a previously defined function,
  281. the following idiom is often used:
  282.      (fset 'old-foo (symbol-function 'foo))
  283.      
  284.      (defun foo ()
  285.        "Just like old-foo, except more so."
  286.        (old-foo)
  287.        (more-so))
  288. This does not work properly if `foo' has been defined to autoload.  In
  289. such a case, when `foo' calls `old-foo', Lisp attempts to define
  290. `old-foo' by loading a file.  Since this presumably defines `foo'
  291. rather than `old-foo', it does not produce the proper results.  The
  292. only way to avoid this problem is to make sure the file is loaded
  293. before moving aside the old definition of `foo'.
  294.    See also the function `indirect-function' in *Note Function
  295. Indirection::.
  296. File: elisp,  Node: Inline Functions,  Next: Related Topics,  Prev: Function Cells,  Up: Functions
  297. Inline Functions
  298. ================
  299.    You can define an "inline function" by using `defsubst' instead of
  300. `defun'.  An inline function works just like an ordinary function
  301. except for one thing: when you compile a call to the function, the
  302. function's definition is open-coded into the caller.
  303.    Making a function inline makes explicit calls run faster.  But it
  304. also has disadvantages.  For one thing, it reduces flexibility; if you
  305. change the definition of the function, calls already inlined still use
  306. the old definition until you recompile them.
  307.    Another disadvantage is that making a large function inline can
  308. increase the size of compiled code both in files and in memory.  Since
  309. the advantages of inline functions are greatest for small functions, you
  310. generally should not make large functions inline.
  311.    It's possible to define a macro to expand into the same code that an
  312. inline function would execute.  But the macro would have a limitation:
  313. you can use it only explicitly--a macro cannot be called with `apply',
  314. `mapcar' and so on.  Also, it takes some work to convert an ordinary
  315. function into a macro.  (*Note Macros::.)  To convert it into an inline
  316. function is very easy; simply replace `defun' with `defsubst'.
  317.    Inline functions can be used and open coded later on in the same
  318. file, following the definition, just like macros.
  319.    Emacs versions prior to 19 did not have inline functions.
  320. File: elisp,  Node: Related Topics,  Prev: Inline Functions,  Up: Functions
  321. Other Topics Related to Functions
  322. =================================
  323.    Here is a table of several functions that do things related to
  324. function calling and function definitions.  They are documented
  325. elsewhere, but we provide cross references here.
  326. `apply'
  327.      See *Note Calling Functions::.
  328. `autoload'
  329.      See *Note Autoload::.
  330. `call-interactively'
  331.      See *Note Interactive Call::.
  332. `commandp'
  333.      See *Note Interactive Call::.
  334. `documentation'
  335.      See *Note Accessing Documentation::.
  336. `eval'
  337.      See *Note Eval::.
  338. `funcall'
  339.      See *Note Calling Functions::.
  340. `ignore'
  341.      See *Note Calling Functions::.
  342. `indirect-function'
  343.      See *Note Function Indirection::.
  344. `interactive'
  345.      See *Note Using Interactive::.
  346. `interactive-p'
  347.      See *Note Interactive Call::.
  348. `mapatoms'
  349.      See *Note Creating Symbols::.
  350. `mapcar'
  351.      See *Note Mapping Functions::.
  352. `mapconcat'
  353.      See *Note Mapping Functions::.
  354. `undefined'
  355.      See *Note Key Lookup::.
  356. File: elisp,  Node: Macros,  Next: Loading,  Prev: Functions,  Up: Top
  357. Macros
  358. ******
  359.    "Macros" enable you to define new control constructs and other
  360. language features.  A macro is defined much like a function, but instead
  361. of telling how to compute a value, it tells how to compute another Lisp
  362. expression which will in turn compute the value.  We call this
  363. expression the "expansion" of the macro.
  364.    Macros can do this because they operate on the unevaluated
  365. expressions for the arguments, not on the argument values as functions
  366. do.  They can therefore construct an expansion containing these
  367. argument expressions or parts of them.
  368.    If you are using a macro to do something an ordinary function could
  369. do, just for the sake of speed, consider using an inline function
  370. instead.  *Note Inline Functions::.
  371. * Menu:
  372. * Simple Macro::            A basic example.
  373. * Expansion::               How, when and why macros are expanded.
  374. * Compiling Macros::        How macros are expanded by the compiler.
  375. * Defining Macros::         How to write a macro definition.
  376. * Backquote::               Easier construction of list structure.
  377. * Problems with Macros::    Don't evaluate the macro arguments too many times.
  378.                               Don't hide the user's variables.
  379. File: elisp,  Node: Simple Macro,  Next: Expansion,  Prev: Macros,  Up: Macros
  380. A Simple Example of a Macro
  381. ===========================
  382.    Suppose we would like to define a Lisp construct to increment a
  383. variable value, much like the `++' operator in C.  We would like to
  384. write `(inc x)' and have the effect of `(setq x (1+ x))'.  Here's a
  385. macro definition that does the job:
  386.      (defmacro inc (var)
  387.         (list 'setq var (list '1+ var)))
  388.    When this is called with `(inc x)', the argument `var' has the value
  389. `x'--*not* the *value* of `x'.  The body of the macro uses this to
  390. construct the expansion, which is `(setq x (1+ x))'.  Once the macro
  391. definition returns this expansion, Lisp proceeds to evaluate it, thus
  392. incrementing `x'.
  393. File: elisp,  Node: Expansion,  Next: Compiling Macros,  Prev: Simple Macro,  Up: Macros
  394. Expansion of a Macro Call
  395. =========================
  396.    A macro call looks just like a function call in that it is a list
  397. which starts with the name of the macro.  The rest of the elements of
  398. the list are the arguments of the macro.
  399.    Evaluation of the macro call begins like evaluation of a function
  400. call except for one crucial difference: the macro arguments are the
  401. actual expressions appearing in the macro call.  They are not evaluated
  402. before they are given to the macro definition.  By contrast, the
  403. arguments of a function are results of evaluating the elements of the
  404. function call list.
  405.    Having obtained the arguments, Lisp invokes the macro definition just
  406. as a function is invoked.  The argument variables of the macro are bound
  407. to the argument values from the macro call, or to a list of them in the
  408. case of a `&rest' argument.  And the macro body executes and returns
  409. its value just as a function body does.
  410.    The second crucial difference between macros and functions is that
  411. the value returned by the macro body is not the value of the macro call.
  412. Instead, it is an alternate expression for computing that value, also
  413. known as the "expansion" of the macro.  The Lisp interpreter proceeds
  414. to evaluate the expansion as soon as it comes back from the macro.
  415.    Since the expansion is evaluated in the normal manner, it may contain
  416. calls to other macros.  It may even be a call to the same macro, though
  417. this is unusual.
  418.    You can see the expansion of a given macro call by calling
  419. `macroexpand'.
  420.  - Function: macroexpand FORM &optional ENVIRONMENT
  421.      This function expands FORM, if it is a macro call.  If the result
  422.      is another macro call, it is expanded in turn, until something
  423.      which is not a macro call results.  That is the value returned by
  424.      `macroexpand'.  If FORM is not a macro call to begin with, it is
  425.      returned as given.
  426.      Note that `macroexpand' does not look at the subexpressions of
  427.      FORM (although some macro definitions may do so).  Even if they
  428.      are macro calls themselves, `macroexpand' does not expand them.
  429.      The function `macroexpand' does not expand calls to inline
  430.      functions.  Normally there is no need for that, since a call to an
  431.      inline function is no harder to understand than a call to an
  432.      ordinary function.
  433.      If ENVIRONMENT is provided, it specifies an alist of macro
  434.      definitions that shadow the currently defined macros.  This is used
  435.      by byte compilation.
  436.           (defmacro inc (var)
  437.               (list 'setq var (list '1+ var)))
  438.                => inc
  439.           (macroexpand '(inc r))
  440.                => (setq r (1+ r))
  441.           (defmacro inc2 (var1 var2)
  442.               (list 'progn (list 'inc var1) (list 'inc var2)))
  443.                => inc2
  444.           (macroexpand '(inc2 r s))
  445.                => (progn (inc r) (inc s))  ; `inc' not expanded here.
  446. File: elisp,  Node: Compiling Macros,  Next: Defining Macros,  Prev: Expansion,  Up: Macros
  447. Macros and Byte Compilation
  448. ===========================
  449.    You might ask why we take the trouble to compute an expansion for a
  450. macro and then evaluate the expansion.  Why not have the macro body
  451. produce the desired results directly?  The reason has to do with
  452. compilation.
  453.    When a macro call appears in a Lisp program being compiled, the Lisp
  454. compiler calls the macro definition just as the interpreter would, and
  455. receives an expansion.  But instead of evaluating this expansion, it
  456. compiles the expansion as if it had appeared directly in the program.
  457. As a result, the compiled code produces the value and side effects
  458. intended for the macro, but executes at full compiled speed.  This would
  459. not work if the macro body computed the value and side effects
  460. itself--they would be computed at compile time, which is not useful.
  461.    In order for compilation of macro calls to work, the macros must be
  462. defined in Lisp when the calls to them are compiled.  The compiler has a
  463. special feature to help you do this: if a file being compiled contains a
  464. `defmacro' form, the macro is defined temporarily for the rest of the
  465. compilation of that file.  To use this feature, you must define the
  466. macro in the same file where it is used and before its first use.
  467.    While byte-compiling a file, any `require' calls at top-level are
  468. executed.  One way to ensure that necessary macro definitions are
  469. available during compilation is to require the file that defines them.
  470. *Note Features::.
  471. File: elisp,  Node: Defining Macros,  Next: Backquote,  Prev: Compiling Macros,  Up: Macros
  472. Defining Macros
  473. ===============
  474.    A Lisp macro is a list whose CAR is `macro'.  Its CDR should be a
  475. function; expansion of the macro works by applying the function (with
  476. `apply') to the list of unevaluated argument-expressions from the macro
  477. call.
  478.    It is possible to use an anonymous Lisp macro just like an anonymous
  479. function, but this is never done, because it does not make sense to pass
  480. an anonymous macro to mapping functions such as `mapcar'.  In practice,
  481. all Lisp macros have names, and they are usually defined with the
  482. special form `defmacro'.
  483.  - Special Form: defmacro NAME ARGUMENT-LIST BODY-FORMS...
  484.      `defmacro' defines the symbol NAME as a macro that looks like this:
  485.           (macro lambda ARGUMENT-LIST . BODY-FORMS)
  486.      This macro object is stored in the function cell of NAME.  The
  487.      value returned by evaluating the `defmacro' form is NAME, but
  488.      usually we ignore this value.
  489.      The shape and meaning of ARGUMENT-LIST is the same as in a
  490.      function, and the keywords `&rest' and `&optional' may be used
  491.      (*note Argument List::.).  Macros may have a documentation string,
  492.      but any `interactive' declaration is ignored since macros cannot be
  493.      called interactively.
  494. File: elisp,  Node: Backquote,  Next: Problems with Macros,  Prev: Defining Macros,  Up: Macros
  495. Backquote
  496. =========
  497.    It could prove rather awkward to write macros of significant size,
  498. simply due to the number of times the function `list' needs to be
  499. called.  To make writing these forms easier, a macro ``' (often called
  500. "backquote") exists.
  501.    Backquote allows you to quote a list, but selectively evaluate
  502. elements of that list.  In the simplest case, it is identical to the
  503. special form `quote' (*note Quoting::.).  For example, these two forms
  504. yield identical results:
  505.      (` (a list of (+ 2 3) elements))
  506.           => (a list of (+ 2 3) elements)
  507.      (quote (a list of (+ 2 3) elements))
  508.           => (a list of (+ 2 3) elements)
  509.    By inserting a special marker, `,', inside of the argument to
  510. backquote, it is possible to evaluate desired portions of the argument:
  511.      (list 'a 'list 'of (+ 2 3) 'elements)
  512.           => (a list of 5 elements)
  513.      (` (a list of (, (+ 2 3)) elements))
  514.           => (a list of 5 elements)
  515.    It is also possible to have an evaluated list "spliced" into the
  516. resulting list by using the special marker `,@'.  The elements of the
  517. spliced list become elements at the same level as the other elements of
  518. the resulting list.  The equivalent code without using ``' is often
  519. unreadable.  Here are some examples:
  520.      (setq some-list '(2 3))
  521.           => (2 3)
  522.      (cons 1 (append some-list '(4) some-list))
  523.           => (1 2 3 4 2 3)
  524.      (` (1 (,@ some-list) 4 (,@ some-list)))
  525.           => (1 2 3 4 2 3)
  526.      
  527.      (setq list '(hack foo bar))
  528.           => (hack foo bar)
  529.      (cons 'use
  530.        (cons 'the
  531.          (cons 'words (append (cdr list) '(as elements)))))
  532.           => (use the words foo bar as elements)
  533.      (` (use the words (,@ (cdr list)) as elements (,@ nil)))
  534.           => (use the words foo bar as elements)
  535.    The reason for `(,@ nil)' is to avoid a bug in Emacs version 18.
  536. The bug occurs when a call to `,@' is followed only by constant
  537. elements.  Thus,
  538.      (` (use the words (,@ (cdr list)) as elements))
  539. would not work, though it really ought to.  `(,@ nil)' avoids the
  540. problem by being a nonconstant element that does not affect the result.
  541.  - Macro: ` LIST
  542.      This macro returns LIST as `quote' would, except that the list is
  543.      copied each time this expression is evaluated, and any sublist of
  544.      the form `(, SUBEXP)' is replaced by the value of SUBEXP.  Any
  545.      sublist of the form `(,@ LISTEXP)' is replaced by evaluating
  546.      LISTEXP and splicing its elements into the containing list in
  547.      place of this sublist.  (A single sublist can in this way be
  548.      replaced by any number of new elements in the containing list.)
  549.      There are certain contexts in which `,' would not be recognized and
  550.      should not be used:
  551.           ;; Use of a `,' expression as the CDR of a list.
  552.           (` (a . (, 1)))                             ; Not `(a . 1)'
  553.                => (a \, 1)
  554.           ;; Use of `,' in a vector.
  555.           (` [a (, 1) c])                             ; Not `[a 1 c]'
  556.                error--> Wrong type argument
  557.           ;; Use of a `,' as the entire argument of ``'.
  558.           (` (, 2))                                   ; Not 2
  559.                => (\, 2)
  560.      Common Lisp note: in Common Lisp, `,' and `,@' are implemented as
  561.      reader macros, so they do not require parentheses.  Emacs Lisp
  562.      implements them as functions because reader macros are not
  563.      supported (to save space).
  564. File: elisp,  Node: Problems with Macros,  Prev: Backquote,  Up: Macros
  565. Common Problems Using Macros
  566. ============================
  567.    The basic facts of macro expansion have all been described above, but
  568. there consequences are often counterintuitive.  This section describes
  569. some important consequences that can lead to trouble, and rules to
  570. follow to avoid trouble.
  571. * Menu:
  572. * Argument Evaluation::    The expansion should evaluate each macro arg once.
  573. * Surprising Local Vars::  Local variable bindings in the expansion
  574.                               require special care.
  575. * Eval During Expansion::  Don't evaluate them; put them in the expansion.
  576. * Repeated Expansion::     Avoid depending on how many times expansion is done.
  577. File: elisp,  Node: Argument Evaluation,  Next: Surprising Local Vars,  Prev: Problems with Macros,  Up: Problems with Macros
  578. Evaluating Macro Arguments Too Many Times
  579. -----------------------------------------
  580.    When defining a macro you must pay attention to the number of times
  581. the arguments will be evaluated when the expansion is executed.  The
  582. following macro (used to facilitate iteration) illustrates the problem.
  583. This macro allows us to write a simple "for" loop such as one might
  584. find in Pascal.
  585.      (defmacro for (var from init to final do &rest body)
  586.        "Execute a simple \"for\" loop, e.g.,
  587.          (for i from 1 to 10 do (print i))."
  588.        (list 'let (list (list var init))
  589.              (cons 'while (cons (list '<= var final)
  590.                                 (append body (list (list 'inc var)))))))
  591.      => for
  592.      (for i from 1 to 3 do
  593.         (setq square (* i i))
  594.         (princ (format "\n%d %d" i square)))
  595.      ==>
  596.      (let ((i 1))
  597.        (while (<= i 3)
  598.          (setq square (* i i))
  599.          (princ (format "%d      %d" i square))
  600.          (inc i)))
  601.      -|1       1
  602.           -|2       4
  603.           -|3       9
  604.      => nil
  605. (The arguments `from', `to', and `do' in this macro are "syntactic
  606. sugar"; they are entirely ignored.  The idea is that you will write
  607. noise words (such as `from', `to', and `do') in those positions in the
  608. macro call.)
  609.    This macro suffers from the defect that FINAL is evaluated on every
  610. iteration.  If FINAL is a constant, this is not a problem.  If it is a
  611. more complex form, say `(long-complex-calculation x)', this can slow
  612. down the execution significantly.  If FINAL has side effects, executing
  613. it more than once is probably incorrect.
  614.    A well-designed macro definition takes steps to avoid this problem by
  615. producing an expansion that evaluates the argument expressions exactly
  616. once unless repeated evaluation is part of the intended purpose of the
  617. macro.  Here is a correct expansion for the `for' macro:
  618.      (let ((i 1)
  619.            (max 3))
  620.        (while (<= i max)
  621.          (setq square (* i i))
  622.          (princ (format "%d      %d" i square))
  623.          (inc i)))
  624.    Here is a macro definition that creates this expansion:
  625.      (defmacro for (var from init to final do &rest body)
  626.        "Execute a simple for loop: (for i from 1 to 10 do (print i))."
  627.        (` (let (((, var) (, init))
  628.                 (max (, final)))
  629.             (while (<= (, var) max)
  630.               (,@ body)
  631.               (inc (, var))))))
  632.    Unfortunately, this introduces another problem.  Proceed to the
  633. following node.
  634. File: elisp,  Node: Surprising Local Vars,  Next: Eval During Expansion,  Prev: Argument Evaluation,  Up: Problems with Macros
  635. Local Variables in Macro Expansions
  636. -----------------------------------
  637.    In the previous section, the definition of `for' was fixed as
  638. follows to make the expansion evaluate the macro arguments the proper
  639. number of times:
  640.      (defmacro for (var from init to final do &rest body)
  641.        "Execute a simple for loop: (for i from 1 to 10 do (print i))."
  642.      (` (let (((, var) (, init))
  643.                 (max (, final)))
  644.             (while (<= (, var) max)
  645.               (,@ body)
  646.               (inc (, var))))))
  647.    The new definition of `for' has a new problem: it introduces a local
  648. variable named `max' which the user does not expect.  This causes
  649. trouble in examples such as the following:
  650.      (let ((max 0))
  651.        (for x from 0 to 10 do
  652.          (let ((this (frob x)))
  653.            (if (< max this)
  654.                (setq max this)))))
  655. The references to `max' inside the body of the `for', which are
  656. supposed to refer to the user's binding of `max', really access the
  657. binding made by `for'.
  658.    The way to correct this is to use an uninterned symbol instead of
  659. `max' (*note Creating Symbols::.).  The uninterned symbol can be bound
  660. and referred to just like any other symbol, but since it is created by
  661. `for', we know that it cannot appear in the user's program.  Since it
  662. is not interned, there is no way the user can put it into the program
  663. later.  It will never appear anywhere except where put by `for'.  Here
  664. is a definition of `for' which works this way:
  665.      (defmacro for (var from init to final do &rest body)
  666.        "Execute a simple for loop: (for i from 1 to 10 do (print i))."
  667.        (let ((tempvar (make-symbol "max")))
  668.          (` (let (((, var) (, init))
  669.                   ((, tempvar) (, final)))
  670.               (while (<= (, var) (, tempvar))
  671.                      (,@ body)
  672.                      (inc (, var)))))))
  673. This creates an uninterned symbol named `max' and puts it in the
  674. expansion instead of the usual interned symbol `max' that appears in
  675. expressions ordinarily.
  676. File: elisp,  Node: Eval During Expansion,  Next: Repeated Expansion,  Prev: Surprising Local Vars,  Up: Problems with Macros
  677. Evaluating Macro Arguments in Expansion
  678. ---------------------------------------
  679.    Another problem can happen if you evaluate any of the macro argument
  680. expressions during the computation of the expansion, such as by calling
  681. `eval' (*note Eval::.).  If the argument is supposed to refer to the
  682. user's variables, you may have trouble if the user happens to use a
  683. variable with the same name as one of the macro arguments.  Inside the
  684. macro body, the macro argument binding is the most local binding of this
  685. variable, so any references inside the form being evaluated do refer to
  686. it.  Here is an example:
  687.      (defmacro foo (a)
  688.        (list 'setq (eval a) t))
  689.           => foo
  690.      (setq x 'b)
  691.      (foo x) ==> (setq b t)
  692.           => t                  ; and `b' has been set.
  693.      ;; but
  694.      (setq a 'b)
  695.      (foo a) ==> (setq 'b t)     ; invalid!
  696.      error--> Symbol's value is void: b
  697.    It makes a difference whether the user types `a' or `x', because `a'
  698. conflicts with the macro argument variable `a'.
  699.    In general it is best to avoid calling `eval' in a macro definition
  700. at all.
  701. File: elisp,  Node: Repeated Expansion,  Prev: Eval During Expansion,  Up: Problems with Macros
  702. How Many Times is the Macro Expanded?
  703. -------------------------------------
  704.    Occasionally problems result from the fact that a macro call is
  705. expanded each time it is evaluated in an interpreted function, but is
  706. expanded only once (during compilation) for a compiled function.  If the
  707. macro definition has side effects, they will work differently depending
  708. on how many times the macro is expanded.
  709.    In particular, constructing objects is a kind of side effect.  If the
  710. macro is called once, then the objects are constructed only once.  In
  711. other words, the same structure of objects is used each time the macro
  712. call is executed.  In interpreted operation, the macro is reexpanded
  713. each time, producing a fresh collection of objects each time.  Usually
  714. this does not matter--the objects have the same contents whether they
  715. are shared or not.  But if the surrounding program does side effects on
  716. the objects, it makes a difference whether they are shared.  Here is an
  717. example:
  718.      (defmacro new-object ()
  719.        (list 'quote (cons nil nil)))
  720.      (defun initialize (condition)
  721.        (let ((object (new-object)))
  722.          (if condition
  723.          (setcar object condition))
  724.          object))
  725. If `initialize' is interpreted, a new list `(nil)' is constructed each
  726. time `initialize' is called.  Thus, no side effect survives between
  727. calls.  If `initialize' is compiled, then the macro `new-object' is
  728. expanded during compilation, producing a single "constant" `(nil)' that
  729. is reused and altered each time `initialize' is called.
  730. File: elisp,  Node: Loading,  Next: Byte Compilation,  Prev: Macros,  Up: Top
  731. Loading
  732. *******
  733.    Loading a file of Lisp code means bringing its contents into the Lisp
  734. environment in the form of Lisp objects.  Emacs finds and opens the
  735. file, reads the text, evaluates each form, and then closes the file.
  736.    The load functions evaluate all the expressions in a file just as
  737. the `eval-current-buffer' function evaluates all the expressions in a
  738. buffer.  The difference is that the load functions read and evaluate
  739. the text in the file as found on disk, not the text in an Emacs buffer.
  740.    The loaded file must contain Lisp expressions, either as source code
  741. or, optionally, as byte-compiled code.  Each form in the file is called
  742. a "top-level form".  There is no special format for the forms in a
  743. loadable file; any form in a file may equally well be typed directly
  744. into a buffer and evaluated there.  (Indeed, most code is tested this
  745. way.)  Most often, the forms are function definitions and variable
  746. definitions.
  747.    A file containing Lisp code is often called a "library".  Thus, the
  748. "Rmail library" is a file containing code for Rmail mode.  Similarly, a
  749. "Lisp library directory" is a directory of files containing Lisp code.
  750. * Menu:
  751. * How Programs Do Loading::     The `load' function and others.
  752. * Autoload::                    Setting up a function to autoload.
  753. * Repeated Loading::            Precautions about loading a file twice.
  754. * Features::                    Loading a library if it isn't already loaded.
  755. * Unloading::            How to "unload" a library that was loaded.
  756. * Hooks for Loading::        Providing code to be run when
  757.                   particular libraries are loaded.
  758. File: elisp,  Node: How Programs Do Loading,  Next: Autoload,  Up: Loading
  759. How Programs Do Loading
  760. =======================
  761.    There are several interface functions for loading.  For example, the
  762. `autoload' function creates a Lisp object that loads a file when it is
  763. evaluated (*note Autoload::.).  `require' also causes files to be
  764. loaded (*note Features::.).  Ultimately, all these facilities call the
  765. `load' function to do the work.
  766.  - Function: load FILENAME &optional MISSING-OK NOMESSAGE NOSUFFIX
  767.      This function finds and opens a file of Lisp code, evaluates all
  768.      the forms in it, and closes the file.
  769.      To find the file, `load' first looks for a file named
  770.      `FILENAME.elc', that is, for a file whose name has `.elc'
  771.      appended.  If such a file exists, it is loaded.  But if there is
  772.      no file by that name, then `load' looks for a file whose name has
  773.      `.el' appended.  If that file exists, it is loaded.  Finally, if
  774.      there is no file by either name, `load' looks for a file named
  775.      FILENAME with nothing appended, and loads it if it exists.  (The
  776.      `load' function is not clever about looking at FILENAME.  In the
  777.      perverse case of a file named `foo.el.el', evaluation of `(load
  778.      "foo.el")' will indeed find it.)
  779.      If the optional argument NOSUFFIX is non-`nil', then the suffixes
  780.      `.elc' and `.el' are not tried.  In this case, you must specify
  781.      the precise file name you want.
  782.      If FILENAME is a relative file name, such as `foo' or
  783.      `baz/foo.bar', `load' searches for the file using the variable
  784.      `load-path'.  It appends FILENAME to each of the directories
  785.      listed in `load-path', and loads the first file it finds whose
  786.      name matches.  The current default directory is tried only if it is
  787.      specified in `load-path', where it is represented as `nil'.  All
  788.      three possible suffixes are tried in the first directory in
  789.      `load-path', then all three in the second directory in
  790.      `load-path', etc.
  791.      If you get a warning that `foo.elc' is older than `foo.el', it
  792.      means you should consider recompiling `foo.el'.  *Note Byte
  793.      Compilation::.
  794.      Messages like `Loading foo...' and `Loading foo...done' appear in
  795.      the echo area during loading unless NOMESSAGE is non-`nil'.
  796.      Any errors that are encountered while loading a file cause `load'
  797.      to abort.  If the load was done for the sake of `autoload', certain
  798.      kinds of top-level forms, those which define functions, are undone.
  799.      The error `file-error' is signaled (with `Cannot open load file
  800.      FILENAME') if no file is found.  No error is signaled if
  801.      MISSING-OK is non-`nil'--then `load' just returns `nil'.
  802.      `load' returns `t' if the file loads successfully.
  803.  - User Option: load-path
  804.      The value of this variable is a list of directories to search when
  805.      loading files with `load'.  Each element is a string (which must be
  806.      a directory name) or `nil' (which stands for the current working
  807.      directory).  The value of `load-path' is initialized from the
  808.      environment variable `EMACSLOADPATH', if it exists; otherwise it is
  809.      set to the default specified in `emacs/src/paths.h' when Emacs is
  810.      built.
  811.      The syntax of `EMACSLOADPATH' is the same as that of `PATH';
  812.      fields are separated by `:', and `.' is used for the current
  813.      default directory.  Here is an example of how to set your
  814.      `EMACSLOADPATH' variable from a `csh' `.login' file:
  815.           setenv EMACSLOADPATH .:/user/bil/emacs:/usr/local/lib/emacs/lisp
  816.      Here is how to set it using `sh':
  817.           export EMACSLOADPATH
  818.           EMACSLOADPATH=.:/user/bil/emacs:/usr/local/lib/emacs/lisp
  819.      Here is an example of code you can place in a `.emacs' file to add
  820.      several directories to the front of your default `load-path':
  821.           (setq load-path
  822.                 (append
  823.                  (list nil
  824.                        "/user/bil/emacs"
  825.                        "/usr/local/lisplib")
  826.                  load-path))
  827.      In this example, the path searches the current working directory
  828.      first, followed then by the `/user/bil/emacs' directory and then by
  829.      the `/usr/local/lisplib' directory, which are then followed by the
  830.      standard directories for Lisp code.
  831.      When Emacs version 18 processes command options `-l' or `-load'
  832.      which specify Lisp libraries to be loaded, it temporarily adds the
  833.      current directory to the front of `load-path' so that files in the
  834.      current directory can be specified easily.  Newer Emacs versions
  835.      also find such files in the current directory, but without
  836.      altering `load-path'.
  837.  - Variable: load-in-progress
  838.      This variable is non-`nil' if Emacs is in the process of loading a
  839.      file, and it is `nil' otherwise.  This is how `defun' and
  840.      `provide' determine whether a load is in progress, so that their
  841.      effect can be undone if the load fails.
  842.    To learn how `load' is used to build Emacs, see *Note Building
  843. Emacs::.
  844. File: elisp,  Node: Autoload,  Next: Repeated Loading,  Prev: How Programs Do Loading,  Up: Loading
  845. Autoload
  846. ========
  847.    The "autoload" facility allows you to make a function or macro
  848. available but put off loading its actual definition.  An attempt to call
  849. a symbol whose definition is an autoload object automatically reads the
  850. file to install the real definition and its other associated code, and
  851. then calls the real definition.
  852.    To prepare a function or macro for autoloading, you must call
  853. `autoload', specifying the function name and the name of the file to be
  854. loaded.  A file such as `emacs/lisp/loaddefs.el' usually does this when
  855. Emacs is first built.
  856.    The following example shows how `doctor' is prepared for autoloading
  857. in `loaddefs.el':
  858.      (autoload 'doctor "doctor"
  859.        "\
  860.      Switch to *doctor* buffer and start giving psychotherapy."
  861.        t)
  862. The backslash and newline immediately following the double-quote are a
  863. convention used only in the preloaded Lisp files such as `loaddefs.el';
  864. they cause the documentation string to be put in the `etc/DOC' file.
  865. (*Note Building Emacs::.)  In any other source file, you would write
  866. just this:
  867.      (autoload 'doctor "doctor"
  868.        "Switch to *doctor* buffer and start giving psychotherapy."
  869.        t)
  870.    Calling `autoload' creates an autoload object containing the name of
  871. the file and some other information, and makes this the function
  872. definition of the specified symbol.  When you later try to call that
  873. symbol as a function or macro, the file is loaded; the loading should
  874. redefine that symbol with its proper definition.  After the file
  875. completes loading, the function or macro is called as if it had been
  876. there originally.
  877.    If, at the end of loading the file, the desired Lisp function or
  878. macro has not been defined, then the error `error' is signaled (with
  879. data `"Autoloading failed to define function FUNCTION-NAME"').
  880.    The autoloaded file may, of course, contain other definitions and may
  881. require or provide one or more features.  If the file is not completely
  882. loaded (due to an error in the evaluation of the contents) any function
  883. definitions or `provide' calls that occurred during the load are
  884. undone.  This is to ensure that the next attempt to call any function
  885. autoloading from this file will try again to load the file.  If not for
  886. this, then some of the functions in the file might appear defined, but
  887. they may fail to work properly for the lack of certain subroutines
  888. defined later in the file and not loaded successfully.
  889.    Emacs as distributed comes with many autoloaded functions.  The
  890. calls to `autoload' are in the file `loaddefs.el'.  There is a
  891. convenient way of updating them automatically.
  892.    Write `;;;###autoload' on a line by itself before a function
  893. definition before the real definition of the function, in its
  894. autoloadable source file; then the command `M-x update-file-autoloads'
  895. automatically puts the `autoload' call into `loaddefs.el'.  `M-x
  896. update-directory-autoloads' is more powerful; it updates autoloads for
  897. all files in the current directory.
  898.    You can also put other kinds of forms into `loaddefs.el', by writing
  899. `;;;###autoload' followed on the same line by the form.  `M-x
  900. update-file-autoloads' copies the form from that line.
  901.    The commands for updating autoloads work by visiting and editing the
  902. file `loaddefs.el'.  To make the result take effect, you must save that
  903. file's buffer.
  904.  - Function: autoload SYMBOL FILENAME &optional DOCSTRING INTERACTIVE
  905.           TYPE
  906.      This function defines the function (or macro) named SYMBOL so as
  907.      to load automatically from FILENAME.  The string FILENAME is a
  908.      file name which will be passed to `load' when the function is
  909.      called.
  910.      The argument DOCSTRING is the documentation string for the
  911.      function.  Normally, this is the same string that is in the
  912.      function definition itself.  This makes it possible to look at the
  913.      documentation without loading the real definition.
  914.      If INTERACTIVE is non-`nil', then the function can be called
  915.      interactively.  This lets completion in `M-x' work without loading
  916.      the function's real definition.  The complete interactive
  917.      specification need not be given here.  If TYPE is `macro', then
  918.      the function is really a macro.  If TYPE is `keymap', then the
  919.      function is really a keymap.
  920.      If SYMBOL already has a non-`nil' function definition that is not
  921.      an autoload object, `autoload' does nothing and returns `nil'.  If
  922.      the function cell of SYMBOL is void, or is already an autoload
  923.      object, then it is set to an autoload object that looks like this:
  924.           (autoload FILENAME DOCSTRING INTERACTIVE TYPE)
  925.      For example,
  926.           (symbol-function 'run-prolog)
  927.                => (autoload "prolog" 169681 t nil)
  928.      In this case, `"prolog"' is the name of the file to load, 169681
  929.      refers to the documentation string in the `emacs/etc/DOC' file
  930.      (*note Documentation Basics::.), `t' means the function is
  931.      interactive, and `nil' that it is not a macro.
  932. File: elisp,  Node: Repeated Loading,  Next: Features,  Prev: Autoload,  Up: Loading
  933. Repeated Loading
  934. ================
  935.    You may load a file more than once in an Emacs session.  For
  936. example, after you have rewritten and reinstalled a function definition
  937. by editing it in a buffer, you may wish to return to the original
  938. version; you can do this by reloading the file in which it is located.
  939.    When you load or reload files, bear in mind that the `load' and
  940. `load-library' functions automatically load a byte-compiled file rather
  941. than a non-compiled file of similar name.  If you rewrite a file that
  942. you intend to save and reinstall, remember to byte-compile it if
  943. necessary; otherwise you may find yourself inadvertently reloading the
  944. older, byte-compiled file instead of your newer, non-compiled file!
  945.    When writing the forms in a library, keep in mind that the library
  946. might be loaded more than once.  For example, the choice of `defvar'
  947. vs. `defconst' for defining a variable depends on whether it is
  948. desirable to reinitialize the variable if the library is reloaded:
  949. `defconst' does so, and `defvar' does not.  (*Note Defining
  950. Variables::.)
  951.    The simplest way to add an element to an alist is like this:
  952.      (setq minor-mode-alist
  953.            (cons '(leif-mode " Leif") minor-mode-alist))
  954. But this would add multiple elements if the library is reloaded.  To
  955. avoid the problem, write this:
  956.      (or (assq 'leif-mode minor-mode-alist)
  957.          (setq minor-mode-alist
  958.                (cons '(leif-mode " Leif") minor-mode-alist)))
  959.    Occasionally you will want to test explicitly whether a library has
  960. already been loaded; you can do so as follows:
  961.      (if (not (boundp 'foo-was-loaded))
  962.          EXECUTE-FIRST-TIME-ONLY)
  963.      
  964.      (setq foo-was-loaded t)
  965.